package com.longrise.android.byjk.plugins.course.tiku;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.webkit.WebView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.utils.GetWebUrlHelper;
import com.longrise.common.base.web.BaseWebActivity;
import com.longrise.common.base.web.BaseWebBridge;
import com.longrise.common.base.web.BaseWebClient;

/**
 * Created by callxkj on 2017/10/27.
 * 题库
 */

public class TikuActivity extends BaseWebActivity {

    private WebView mWv;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.web_comment;
    }

    @Override
    public WebView getWebView() {
        return mWv;
    }

    @Override
    public void init() {
        mWv = (WebView) findViewById(R.id.comment_wv);
    }

    @Override
    public void initData() {
        GetWebUrlHelper.getWebUrl(GetWebUrlHelper.MRYL, new GetWebUrlHelper.OnGetWebUrlResultListener() {
            @Override
            public void onSuccess(String url) {
                loadUrl(url);
            }

            @Override
            public void onError(String errormsg) {
                showToast(errormsg);
            }
        });
    }

    @Override
    public BaseWebBridge getwebBridge() {
        return new TikuBridge(this, mRxmanager);
    }

    @Override
    public BaseWebClient getWebClient() {
        return new TikuWebClient(this);
    }

    /**
     * 调用web写的返回方法
     */
    @Override
    public void onToolbarBackClick() {
       goH5back();
    }

    @Override
    public void onToolbarRightIvClick() {
        if (checkNetWorkEnable()) {
            mWv.post(new Runnable() {
                @Override
                public void run() {
                    mWv.loadUrl("javascript:$_$.showDateBox()");
                }
            });
        }
    }

    /**
     * 重写onKeyDown 返回调用web的返回
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        try {
            if (keyCode == KeyEvent.KEYCODE_BACK && mWv.canGoBack()) {
                goH5back();
                return true;
            }
            if (event == null) {
                finish();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 设置右上角的日历按钮
     *
     * @param flag true:显示  false:隐藏
     */
    public void setRightIcon(boolean flag) {
        if(flag){
            setToolbarRightIvTitle(R.drawable.by_ic_test_date);
        }else{
            hideToolbarRightIvTitle();
        }
    }
}
